package com.setin.game;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class GameSampleActivity extends ListActivity  {

	
	List<String> lottoListView = new ArrayList<String>();
	ListView listView ;
	TextView textView;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button startButton = (Button) findViewById(R.id.button7);

		startButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				List<String> lottoNumber = makeLottoNumber();
				Button button1 = (Button) findViewById(R.id.button1);
				Button button2 = (Button) findViewById(R.id.button2);
				Button button3 = (Button) findViewById(R.id.button3);
				Button button4 = (Button) findViewById(R.id.button4);
				Button button5 = (Button) findViewById(R.id.button5);
				Button button6 = (Button) findViewById(R.id.button6);
				textView = (TextView) findViewById(R.id.textView1);
				Iterator<String> iter = lottoNumber.iterator();
				while (iter.hasNext()) {
					button1.setText(iter.next());
					button2.setText(iter.next());
					button3.setText(iter.next());
					button4.setText(iter.next());
					button5.setText(iter.next());
					button6.setText(iter.next());
				}
				
				String lottoNo = button1.getText().toString() + " ,  " +   button2.getText().toString() + " ,  "+
				   button3.getText().toString() + " ,  "+
				   button4.getText().toString() + " ,  "+
				   button5.getText().toString() + " ,  "+
				   button6.getText().toString();
				
				lottoListView.add(lottoNo);
				setListView();	
			}
			
		});
	}

	public List<String> makeLottoNumber() {
		List<String> list = new ArrayList<String>();
		Random random = new Random();
		boolean match = false;
		while (list.size() != 6) {
			Integer randomValue = random.nextInt(45)+1;
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).equals(randomValue.toString())) {
					match = true;
				}
			}
			if (match == false) {
				list.add(randomValue.toString());
				System.out.println(randomValue);
			}
			match = false;
		}
		return list;
	}
	private void setListView(){
		
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list,R.id.listentry, lottoListView));
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		textView.setText(lottoListView.get(position));
	}

}